/**
 * Created by FreezeSoul on 2016/6/8.
 */
define([
    "app/core/base",
    "../../util",
    "jquery",
    "underscore",
    'JSONParse',
    'loglevel'
], function (Chart, Util, $, _, JSON, log) {

    var property = {
        config: "property",
        option: []
    };

    var useredit = Chart.extend({
        constructor: function (layout) {
            this._layout = layout;
            this._element = null;
            this._container = null;
            this._option = {
                Binding: [],
                Events: [],
                Extend: $.extend(true, {}, property),
                Option: {}
            };
        },
        init: function (element) {
            try {
                this._container = element;
            } catch (e) {
                log.error(e);
            }
        },
        _render: function () {
            var _this = this;
            $(this._container).empty();
            var htmlStr = '<div class="panel panel-custom" style="position: relative; text-align: center;">';
            htmlStr += '        <div class="panel-heading">用户信息编辑</div>';
            htmlStr += '        <div class="panel-body" style="padding: 0 20px;margin: 10px;">';
            htmlStr += '             <div class="row">';
            htmlStr += '                <div class="form-group">';
            htmlStr += '                    <label for="user_name" class="control-label col-xs-3">用户名称</label>';
            htmlStr += '                    <div class="col-xs-9">';
            htmlStr += '                        <input type="text" class="form-control" name="user_name" id="user_name" placeholder="用户名称">';
            htmlStr += '                    </div>';
            htmlStr += '                </div>';
            htmlStr += '             </div>';
            htmlStr += '             <div class="row">';
            htmlStr += '                <div class="form-group">';
            htmlStr += '                    <label for="user_email" class="control-label col-xs-3">用户邮箱</label>';
            htmlStr += '                    <div class="col-xs-9">';
            htmlStr += '                        <input type="text" class="form-control" name="user_email" id="user_email" placeholder="用户邮箱">';
            htmlStr += '                    </div>';
            htmlStr += '                </div>';
            htmlStr += '             </div>';
            htmlStr += '             <div class="row">';
            htmlStr += '                <div class="form-group">';
            htmlStr += '                    <label for="user_province" class="control-label col-xs-3">所在省份</label>';
            htmlStr += '                    <div class="col-xs-9">';
            htmlStr += '                        <input type="text" class="form-control" name="user_province" id="user_province" placeholder="所在省份">';
            htmlStr += '                    </div>';
            htmlStr += '                </div>';
            htmlStr += '             </div>';
            htmlStr += '             <div class="row">';
            htmlStr += '                <div class="form-group">';
            htmlStr += '                    <label for="user_industry" class="control-label col-xs-3">所属行业</label>';
            htmlStr += '                    <div class="col-xs-9">';
            htmlStr += '                        <input type="text" class="form-control" name="user_industry" id="user_industry" placeholder="所属行业">';
            htmlStr += '                    </div>';
            htmlStr += '                </div>';
            htmlStr += '             </div>';
            htmlStr += '             <div class="row">';
            htmlStr += '                <div class="form-group">';
            htmlStr += '                    <label for="user_occupation" class="control-label col-xs-3">从事职业</label>';
            htmlStr += '                    <div class="col-xs-9">';
            htmlStr += '                        <input type="text" class="form-control" name="user_occupation" id="user_occupation" placeholder="从事职业">';
            htmlStr += '                    </div>';
            htmlStr += '                </div>';
            htmlStr += '             </div>';
            htmlStr += '             <div class="row">';
            htmlStr += '                <div class="form-group">';
            htmlStr += '                    <label for="user_degree" class="control-label col-xs-3">教育水平</label>';
            htmlStr += '                    <div class="col-xs-9">';
            htmlStr += '                        <input type="text" class="form-control" name="user_degree" id="user_degree" placeholder="教育水平">';
            htmlStr += '                    </div>';
            htmlStr += '                </div>';
            htmlStr += '             </div>';
            htmlStr += '        </div>';
            htmlStr += '        <div class="panel-footer" style="position: absolute;width: 100%;bottom: 0;margin-bottom: 0;">';
            htmlStr += '            <button class="btn btn-success" id="SaveUserInfo" type="button">保存</button>';
            htmlStr += '        </div>';
            htmlStr += '   </div>';
            var $htmlStr = $(htmlStr);
            $htmlStr.find('#SaveUserInfo').unbind("click");
            $htmlStr.find('#SaveUserInfo').bind("click", function () {
                var user_name = $htmlStr.find("#user_name").val();
                var user_email = $htmlStr.find("#user_email").val();
                var user_province = $htmlStr.find("#user_province").val();
                var user_industry = $htmlStr.find("#user_industry").val();
                var user_occupation = $htmlStr.find("#user_occupation").val();
                var user_degree = $htmlStr.find("#user_degree").val();
                if (_this._layout.emitter)
                    _this._layout.emitter.emitEvent("microapp.userinfo", [{
                        user_name: user_name,
                        user_email: user_email,
                        user_province: user_province,
                        user_industry: user_industry,
                        user_occupation: user_occupation,
                        user_degree: user_degree
                    }]);
            });
            if (this._layout.emitter)
                this._layout.emitter.addListener("microapp.useredit", function (data) {
                    $htmlStr.find("#user_name").val(data.user_name);
                    $htmlStr.find("#user_email").val(data.user_email);
                    $htmlStr.find("#user_province").val(data.user_province);
                    $htmlStr.find("#user_industry").val(data.user_industry);
                    $htmlStr.find("#user_occupation").val(data.user_occupation);
                    $htmlStr.find("#user_degree").val(data.user_degree);
                });
            var $html = $htmlStr.appendTo(this._container);
            return $html;
        },
        example: function () {
            try {
                this._element = this._render();
            } catch (e) {
                log.error(e);
            }
        },
        _readProperty: function () {
            try {
                if (!this._option.Extend) return;
                Util.deepMapOption(property, this._option.Extend, this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        setData: function (data) {
            try {
                this._readProperty();
                this._element = this._render();
            } catch (e) {
                log.error(e);
            }
        },
        setOption: function (option) {
            try {
                return this._option = $.extend(true, {}, option);
            } catch (e) {
                log.error(e);
            }
        },
        getOption: function () {
            return this._option;
        },
        setTheme: function (theme) {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        resize: function () {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        showLoading: function () {
            try {
                if (this._layout && this._layout.showLoading) {
                    this._layout.showLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        hideLoading: function () {
            try {
                if (this._layout && this._layout.hideLoading) {
                    this._layout.hideLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        dispose: function () {
            try {
                $(this._element).remove();
            } catch (e) {
                log.error(e);
            }
        },
        getChart: function () {
            return this._element;
        }
    });

    return {
        Chart: useredit
    };
});